package com.nd.android.musicboxlibrary.util.fastDateUtil;

import com.nd.android.musicboxlibrary.business.comm.MagicNumber;

import java.util.Calendar;

/**
 * <p>
 * Inner class to output an unpadded number.
 * </p>
 */
public class UnpaddedNumberField implements NumberRule {
    private final int mField;

    /**
     * Constructs an instance of <code>UnpadedNumberField</code> with the specified field.
     * 
     * @param field
     *            the field
     */
    public UnpaddedNumberField(int field) {
        mField = field;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int estimateLength() {
        return MagicNumber.n4;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void appendTo(StringBuffer buffer, Calendar calendar) {
        appendTo(buffer, calendar.get(mField));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final void appendTo(StringBuffer buffer, int value) {
        if (value < MagicNumber.n10) {
            buffer.append((char) (value + '0'));
        } else if (value < MagicNumber.n100) {
            buffer.append((char) (value / MagicNumber.n10 + '0'));
            buffer.append((char) (value % MagicNumber.n10 + '0'));
        } else {
            buffer.append(Integer.toString(value));
        }
    }
}
